from tortoise.models import Model
from tortoise import fields
from pydantic import BaseModel, Field
from typing import Optional


class Class(Model):
    class_id = fields.IntField(pk=True)
    class_name = fields.CharField(max_length=100)
    class_type = fields.CharField(max_length=50)
    price = fields.DecimalField(max_digits=10, decimal_places=2)

    # 一对多关系（保持不变）
    contents = fields.ReverseRelation["Content"]
    files = fields.ReverseRelation["File"]

    # 多对多关系（关键修改）
    enrolled_students = fields.ManyToManyField(
        "models.Student",
        through="models.Record",
        related_name="enrolled_classes",  # 修改这里
        forward_key="stu_id",
        backward_key="class_id"
    )

    class Meta:
        table = "classes"
    class Config:
        from_attributes = True